//
//  CircleDegreeView.m
//  iOSStudy
//
//  Created by Evan Xie on 12/2/15.
//  Copyright © 2015 Family Wings. All rights reserved.
//

#import "CircleDegreeView.h"

@implementation CircleDegreeView

- (void)drawRect:(CGRect)rect {
    
    CGFloat sidePadding = 50;
    CGFloat radius = (MIN(rect.size.width, rect.size.height) - 2*sidePadding) / 2;
    CGPoint center = CGPointMake(sidePadding+radius, sidePadding+radius);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    
    // Draw the center of the circle
    CGContextSaveGState(context);
    CGContextSetFillColorWithColor(context, [UIColor magentaColor].CGColor);
    CGContextAddArc(context, center.x, center.y, 5, 0, 2*M_PI, 0);
    CGContextFillPath(context);
    CGContextRestoreGState(context);
    
    // Draw the circle
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
    CGContextAddArc(context, center.x, center.y, radius, 0, 2*M_PI, 0);
    CGContextStrokePath(context);
    
    // Draw the clockwise arrow
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextAddArc(context, center.x, center.y, radius-20.0, -M_PI_4, M_PI_4, 0);
    CGPoint arrowHead = CGPointMake(center.x+(radius-20)*cosf(M_PI_4), center.y+(radius-20)*sin(M_PI_4));
    CGContextMoveToPoint(context, arrowHead.x-2.0, arrowHead.y-10.0);
    CGContextAddLineToPoint(context, arrowHead.y, arrowHead.y);
    CGContextAddLineToPoint(context, arrowHead.x+16.0, arrowHead.y-5.0);
    CGContextStrokePath(context);

    NSDictionary *fontAttrs = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0], NSForegroundColorAttributeName: [UIColor redColor] };
    [@"0(2π)" drawAtPoint:CGPointMake(center.x+radius+5.0, center.y-5.0) withAttributes:fontAttrs];
    [@"π/2" drawAtPoint:CGPointMake(center.x-10.0, center.y+radius+5.0) withAttributes:fontAttrs];
    [@"π" drawAtPoint:CGPointMake(center.x-radius-20.0, center.y-5.0) withAttributes:fontAttrs];
    [@"3π/2" drawAtPoint:CGPointMake(center.x-10.0, center.y-radius-25.0) withAttributes:fontAttrs];
    
    // Draw bottom left concentric circles
    center = CGPointMake(rect.size.width/2-60, rect.size.height-60);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, center.x, center.y);
    CGContextAddArc(context, center.x, center.y, 50, 0, 2*M_PI, 0);
    CGContextAddArc(context, center.x, center.y, 30, 0, 2*M_PI, 1);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathEOFillStroke);
    
    // Draw bottom right concentric circles
    center = CGPointMake(rect.size.width/2+60, rect.size.height-60);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, center.x, center.y);
    CGPathAddArc(path, NULL, center.x, center.y, 50, 0, 2*M_PI, 0);
    CGPathAddArc(path, NULL, center.x, center.y, 30, 0, 2*M_PI, 0);
    CGPathCloseSubpath(path);
    CGContextAddPath(context, path);
    CGContextDrawPath(context, kCGPathFillStroke);
}

@end
